package com.mew.chess.core.game.yxx.game.core;


import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.common.message.response.ChipMessage;
import com.mew.chess.core.common.utils.RobotNumberUtils;
import com.mew.chess.core.game.yxx.game.YxxGameManager;
import com.mew.chess.core.game.yxx.game.YxxRoomManager;
import com.mew.chess.core.game.yxx.game.common.YxxBetType;
import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.mew.chess.core.game.yxx.game.message.YxxPushBetInfoMessage;
import com.mew.chess.core.game.yxx.game.message.YxxUserMessage;
import com.mew.common.util.GameUtil;

import java.util.Collections;
import java.util.List;

public class YxxRobotBetUtils {

    //求和信息
    public static YxxPushBetInfoMessage sumBetNumber(int roomId, boolean isRobot){
        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);
        //清一下上次押注数组
        YxxPushBetInfoMessage betInfoMessage = roomManager.getBetInfoMessage();
        betInfoMessage.clearInfoList();

        if (isRobot){
            List<YxxUserMessage> robotPlayers = roomManager.getRobotPlayers();
            buildRobotBetInfo(roomManager,robotPlayers,roomManager.getBetArrays());
        }

        return betInfoMessage;

    }

    private static void buildRobotBetInfo(YxxRoomManager roomManager, List<YxxUserMessage> robotPlayers, long[] betArrays){

        Collections.shuffle(robotPlayers);

        int robotNumber = RobotNumberUtils.robotNumber(YxxConstantValue.ROBOT_BET_NUMBER);

        for (int i = 0; i < robotNumber; i++) {
            YxxUserMessage yxxUserMessage = robotPlayers.get(i);
            long uid = yxxUserMessage.getUserId();
            long bet = betArrays[GameUtil.weights(YxxConstantValue.ROBOT_BET_WEIGHTS)];
            long gold = yxxUserMessage.getGoldNumber()-bet;
            if (gold<0){
                continue;
            }
            int type = RandomUtil.randomInt(YxxBetType.values().length);
            yxxUserMessage.getBets().add(ChipMessage.builder().userId(uid).betType(type).betNumber(bet).build());
            yxxUserMessage.setGoldNumber(gold<0?0:gold);
            roomManager.getBetInfoMessage().addBetInfo(uid,type,bet);


        }

    }
}
